﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Telerik.Web.UI;
using Fly.Common.Linq;
using RN.ERP.Web.EnterpriseService;

namespace RN.ERP.Web.CA
{
    public partial class CAInvoiceInfo : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();
        EnterpriseServiceClient enterpriseClient = new EnterpriseServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string t = Request.QueryString["t"];
                if (t != null && t == "1")
                {
                    lbtnConfirm.Enabled = false;
                    txtTime.Enabled = false;
                    txtCode.Enabled = false;
                }
                InitializeWhenView();
            }
        }

        private void InitializeWhenView()
        {
            ddlOptType.Enabled = false;
            ddlType.Enabled = false;
            ddlSendType.Enabled = false;
            ddlInvoiceTitle.Enabled = false;
            string o = Request.QueryString["o"];
            BindData(o);
        }

        private void BindData(string invoiceGuid)
        {
            Guid iGuid = Guid.Empty;
            if (Guid.TryParse(invoiceGuid, out iGuid))
            {
                List<QueryInvoiceInfoCondition> queryList = new List<QueryInvoiceInfoCondition>();
                queryList.Add(new QueryInvoiceInfoCondition()
                {
                    InvoiceGUID = new Fly.Common.Linq.ParameterValue<Guid>() { V = iGuid, O = Fly.Common.Linq.OptEnum.Equal }
                });
                List<ModelService.CaPayRelation> result = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetInvoicesList(JsonConvert.SerializeObject(queryList), 0, 0));
                if (result[0].InvoiceStatus != null && result[0].InvoiceStatus == 1)
                {
                    txtTime.Text = ((DateTime)result[0].InvoiceDate).ToString("yyyy-MM-dd");
                }
                else
                {
                    txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                txtCode.Text = result[0].InvoiceCode;
                ddlOptType.SelectedValue = result[0].InvoiceOptType.ToString();
                ddlType.SelectedValue = result[0].InvoiceType.ToString();
                tr1.Attributes.Clear();
                tr2.Attributes.Clear();
                tr3.Attributes.Clear();
                lbTaxCode.Text = result[0].TaxCode;
                lbBankName.Text = result[0].BankName;
                lbBankCode.Text = result[0].BankCode;
                lbNotice.Text = result[0].Notice;
                lbTitle.Text = result[0].InvoiceTitle;
                lbTel.Text = result[0].InvoiceTel;
                lbMobile.Text = result[0].InvoiceMtel;
                lbLinkMan.Text = result[0].InvoiceLinkMan;
                lbInvoiceAddress.Text = result[0].InvoiceSendAddress;
                lbPostCode.Text = result[0].InvoicePsCode;
                lbAddTime.Text = result[0].IsrtDate.ToString();
                lbSum.Text = result[0].InvoiceSumPay == null ? string.Empty : result[0].InvoiceSumPay.ToString();
                ddlSendType.SelectedValue = result[0].SendType.ToString();
                ddlInvoiceTitle.SelectedValue = result[0].InvoiceContent.ToString();
                RadGrid1.DataSource = result;
                RadGrid1.DataBind();

                //List<QueryCompanyInfoCondition> list = new List<QueryCompanyInfoCondition>();
                //QueryCompanyInfoCondition condition = new QueryCompanyInfoCondition();
                //condition.CorpUserGuid = new ParameterValue<Guid>() { O = OptEnum.Equal, V = result[0].CorpUserGuid };
                //list.Add(condition);
                //var corp = JsonConvert.DeserializeObject<CompanyInfo>(enterpriseClient.GetCompanyBisicInfo(JsonConvert.SerializeObject(list), 
                //    result[0].AreaCode));
                var corp = JsonConvert.DeserializeObject<CompanyInfo>(enterpriseClient.GetCorpInfo(result[0].CorpUserGuid.ToString(),
                    result[0].AreaCode));
                lbAddress.Text = corp.Address;
            }
        }

        protected void lbtnConfirm_Click(object sender, EventArgs e)
        {
            decimal sumPay = 0;
            decimal.TryParse(lbSum.Text, out sumPay);
            InvoiceOrderRelation item = new InvoiceOrderRelation()
            {
                KeyGuid = Guid.Parse(Request.QueryString["o"]),
                InvoiceCode = txtCode.Text.Trim(),
                UpdateDate = DateTime.Now,
                UpdateUser = UserInfo.CorpUserGuid,
                UpdateUserName = UserInfo.UserName,
                InvoiceStatus = 1
            };
            DateTime time = DateTime.Now;
            DateTime.TryParse(txtTime.Text.Trim(), out time);
            item.InvoiceDate = time;
            OperationResult result = JsonConvert.DeserializeObject<OperationResult>(client.AddInvoices(JsonConvert.SerializeObject(item)));
            if (result.Result)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
                //RadWindowManager1.RadAlert(result.Message, 300, 150, "提示", "CloseWin", "");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
                //RadWindowManager1.RadAlert(result.Message, 300, 150, "提示", "");
            }
        }

        protected void RadGrid1_temDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            string str = "";
            int index = 0;
            int intRowSpan = 1;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    str = RadGrid1.Items[i].Cells[2].Text;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        if (RadGrid1.Items[j].Cells[2].Text == str)
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[2].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[2].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }
            index = 0;
            intRowSpan = 1;
            str = "";
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    Label lbId = (Label)RadGrid1.Items[i].Cells[3].FindControl("lbId");
                    str = lbId.Text;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        Label lbId2 = (Label)RadGrid1.Items[j].Cells[3].FindControl("lbId");
                        if (lbId2.Text == str)
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[3].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[3].Visible = false;
                            RadGrid1.Items[i].Cells[4].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[4].Visible = false;
                            RadGrid1.Items[i].Cells[5].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[5].Visible = false;
                            RadGrid1.Items[i].Cells[6].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[6].Visible = false;
                            RadGrid1.Items[i].Cells[7].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[7].Visible = false;
                            RadGrid1.Items[i].Cells[8].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[8].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }
        }
    }
}